package cn.song.controller;

import cn.song.Utils.ResultUtil;
import cn.song.VO.ResultVo;
import cn.song.convert.OrderForm2OrderDto;
import cn.song.dto.OrderDto;
import cn.song.enums.ResultEnum;
import cn.song.exceptions.SellException;
import cn.song.form.OrderForm;
import cn.song.service.BuyerService;
import cn.song.service.OrderService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private BuyerService buyerService;

    //创建订单
    @PostMapping("/create")
    public ResultVo<Map<String,String>> create(@Valid OrderForm orderForm,BindingResult bindingResult){
        if(bindingResult.hasErrors()) {
            log.error("参数不正常,orderForm is {}.", orderForm);
            throw new SellException(ResultEnum.ORDER_PARAMS_ERRRO);
        }
        OrderDto orderDto = OrderForm2OrderDto.convert(orderForm);
        if (CollectionUtils.isEmpty(orderDto.getOrderDetails())) {
            log.error("购物车不能为空");
            throw new SellException(ResultEnum.CARD_EMPTY);

        }

        OrderDto order = orderService.createOrder(orderDto);
        Map<String, String> map = new HashMap<>();
        map.put("orderId",order.getOrderId());

        return ResultUtil.success(map);
    }

    // d订单列表

    @GetMapping("list")
    public  ResultVo<List<OrderDto>> getList(@RequestParam("openid") String openid,
                                             @RequestParam(value = "page",defaultValue = "0")Integer page,
                                             @RequestParam(value = "size",defaultValue = "10")Integer size ){

        if (StringUtils.isEmpty(openid)) {

            log.error("查询订单列表openid is 空 is {}", openid);
            throw new SellException(ResultEnum.OPENID_IS_EMPTY);
        }
        Page<OrderDto> list = orderService.findList(openid, new PageRequest(page, size));
        return  ResultUtil.success(list.getContent());
    }


    //订单详情
    @GetMapping("detail")
    public  ResultVo<OrderDto> getDetail(@RequestParam("openid") String openid,
                                         @RequestParam("orderId") String orderId){

        OrderDto order = buyerService.findOrderOne(openid, orderId);
        return ResultUtil.success(order);
    }


    //取消订单
    @GetMapping("cannel")
    public  ResultVo<OrderDto> cannelOrder(@RequestParam("openid") String openid,
                                           @RequestParam("orderId") String orderId){
        buyerService.cancelOrder(openid, orderId);
        return  ResultUtil.success();
    }

}

